package Algorithm;

public class Algorithm
{
	Vehicle vans[] = new Vehicle[9];
	Vehicle van1 = new Van("Van 1");
	Vehicle van2 = new Van("Van 2");
	Vehicle van3 = new Van("Van 3");
	Vehicle van4 = new Van("Van 4");
	Vehicle van5 = new Van("Van 5");
	Vehicle van6 = new Van("Van 6");
	Vehicle fleetvan1 = new FleetVan("Fleet Van 1");
	Vehicle fleetvan2 = new FleetVan("Fleet Van 2");
	Vehicle fleetvan3 = new FleetVan("Fleet Van 3");

	Project projects[] = new Project[7];
	Project project1 = new Project("Project 1",830, 1130, 6);  //first van
	Project project2 = new Project("Project 2",900, 1100, 9);  //first fleet
	Project project3 = new Project("Project 3",1000, 1130, 7); //second van
	Project project4 = new Project("Project 4",1100, 1400, 4); //third van
	Project project5 = new Project("Project 5",1330, 1700, 5); //first van
	Project project6 = new Project("Project 6",1430, 1600, 10);//first fleet
	Project project7 = new Project("Project 7",1500, 1700, 9); //second fleet

	public static void main(String[] args)
	{
		Algorithm alg = new Algorithm();
		alg.go();
	}

	public void setVans()
	{
		vans[0] = van1;
		vans[1] = van2;
		vans[2] = van3;
		vans[3] = van4;
		vans[4] = van5;
		vans[5] = van6;
		vans[6] = fleetvan1;
		vans[7] = fleetvan2;
		vans[8] = fleetvan3;
	}

	public void setProjects()
	{
		projects[0] = project1;
		projects[1] = project2;
		projects[2] = project3;
		projects[3] = project4;
		projects[4] = project5;
		projects[5] = project6;
		projects[6] = project7;
	}

	public void assignProjects()
	{
		for (int i=0;i<projects.length;i++)
		{
			if (projects[i].getAssignedVehicle() == null)
			{
				for (int j=0;j<vans.length;j++)
				{
					
					while(vans[j].isBrokenDown()) {j++;}
					
					if ((vans[j].getCurrentProject() == null) ||
							(vans[j].getCurrentProject().getTimeEnd() 
									< projects[i].getTimeStart()))
					{
						if (vans[j].getNumOfSeats() >= projects[i].getNumPassengers())
						{
							vans[j].setCurrentProject(projects[i]);
							projects[i].setAssignedVehicle(vans[j]);
							break;
						}
					}
				}
			}
			System.out.println("Assigned van for " + projects[i].getName() + ": " 
					+ projects[i].getAssignedVehicle().getName());
		}
	}

	public void go()
	{
		setVans();
		setProjects();
		vans[1].setBrokenDown(true);
		vans[2].setBrokenDown(true);
		System.out.println("Vans and Projects set\n");
		//System.out.println(vans[2].getName() + " " 
		//+ vans[2].getClass().toString() + " " + vans[2].getNumOfSeats());
		//System.out.println(vans[8].getName() + " " 
		//+ vans[8].getClass().toString() + " " + vans[8].getNumOfSeats());		
		assignProjects();
	}
}
